package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.common.cacha.DisplayUtil
import com.polaris.live.common.ext.dp
import com.polaris.live.popup.GlobalPopupManager

/**
 * 主页底部导航栏
 *
 * @author Created by 半仙 on 2023/8/15/015 17:11
 */
class TabLayoutView(context: Context, attributeSet: AttributeSet) :
    LinearLayout(context, attributeSet) {

    private var list: ArrayList<View>? = arrayListOf()
    private var lastIndex = -1
    private var images: Array<IntArray>? = null
    private var titles: Array<String>? = null

    fun setTabData(images: Array<IntArray>, titles: Array<String>, currentIndex: Int) {
        this.titles = titles
        this.images = images

        val layoutId = R.layout.diy_tablayout_view

        var i = 0
        while (i < images.size) {
            val itemImages = images[i]
            val view = inflate(context, layoutId, null)
            val tvName = view.findViewById<TextView>(R.id.tablayout_name)
            val ivImage = view.findViewById<ImageView>(R.id.tablayout_img)
            if (ivImage != null) {
                if (currentIndex == i) {
                    ivImage.setImageResource(itemImages[0])
                } else {
                    ivImage.setImageResource(itemImages[1])
                }
            }
            if (tvName != null) {
                tvName.text = titles[i]
                if (currentIndex == i) {
                    tvName.setTextColor(ColorUtils.getColor(R.color.mainTabSelectTextColor))
                } else {
                    tvName.setTextColor(ColorUtils.getColor(R.color.mainTabUnSelectTextColor))
                }
            }
            view.tag = i
            view.layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f)
            view.setOnClickListener { v ->
                val index = v.tag as Int
                if (DisplayUtil.getPrimaryIdByCurrentIndex(index) == DisplayUtil.MINE || DisplayUtil.getPrimaryIdByCurrentIndex(
                        index
                    ) == DisplayUtil.MESSAGE
                ) {
                    GlobalPopupManager.showCompletePopup(context, isSetSex = {
                        lastIndex = -1
                        onRefreshMain?.invoke()
                        setCurrentItem(index)
                        onClickCallBack?.invoke(v, index)

                    }) {
                        setCurrentItem(index)
                        onClickCallBack?.invoke(v, index)
                    }
                } else {
                    setCurrentItem(index)
                    onClickCallBack?.invoke(v, index)
                }
            }
            list?.add(view)
            this.addView(view)
            i++
        }

        val finalIndex = if (lastIndex != -1) {
            lastIndex
        } else {
            0
        }
        setCurrentItem(finalIndex)
    }

    fun initCurrentItem(itemIndex: Int) {
        if (lastIndex == -1) {
            setCurrentItem(itemIndex)
        }
    }

    fun setCurrentItem(itemIndex: Int) {
        val list = list ?: return

        for (i in list.indices) {
            val itemImages = images?.get(i) ?: continue
            val view = list[i]

            val tvName = view.findViewById<TextView>(R.id.tablayout_name)
            val ivImage = view.findViewById<ImageView>(R.id.tablayout_img)
            tvName.isSelected = false
            ivImage.isSelected = false

            if (i == itemIndex) {
                tvName.isSelected = true
                ivImage.isSelected = true
            }

            if (itemIndex == i) {
                //需要动画的话在这里展示动画
                ivImage.setImageResource(itemImages[0])
                tvName.setTextColor(ColorUtils.getColor(R.color.mainTabSelectTextColor))

                if (lastIndex != itemIndex) {
                    val lastView = list.getOrNull(lastIndex)
                    if (lastView != null) {
                        val lastIvImage = lastView.findViewById<ImageView>(R.id.tablayout_img)
                        lastIvImage.visibility = View.VISIBLE
                    }

                    ivImage.visibility = View.VISIBLE
                }
            } else {
                tvName.setTextColor(ColorUtils.getColor(R.color.mainTabUnSelectTextColor))
                ivImage.setImageResource(itemImages[1])
            }

        }
        lastIndex = itemIndex
    }

    fun setNumber(number: Long) {
        DisplayUtil.getMessagePageId()?.let {
            val view = list?.getOrNull(it)
            if (view != null) {
                val textView = view.findViewById<TextView>(R.id.session_read)
                if (number == 0L) {
                    textView.visibility = View.INVISIBLE
                } else {
                    textView.visibility = View.VISIBLE
                    if (number >= 99) {
                        textView.text = "99+"
                    } else {
                        textView.text = "$number"
                    }
                }
            }
        }
    }

    private var meRedCount = 0 //未填资料红点
    fun setMeRed(isShowRed: Boolean) {
        meRedCount = if (isShowRed) {
            1
        } else {
            0
        }
        showRed()
    }

    private var countRed = 0 //游戏红点
    fun setGameCountRed(count: Int) {
        countRed = count
        showRed()
    }

    private fun showRed() {
        DisplayUtil.getMePageId().let {
            val view = list?.getOrNull(it)
            if (view != null) {
                val textView = view.findViewById<TextView>(R.id.session_read)
                when {
                    meRedCount == 1 && countRed == 0 -> {
                        textView.text = ""
                        val layoutParams = textView.layoutParams
                        layoutParams.width = 10.dp
                        layoutParams.height = 10.dp
                        textView.layoutParams = layoutParams
                        textView.visibility = View.VISIBLE
                    }

                    meRedCount == 0 && countRed == 0 -> {
                        textView.visibility = View.GONE
                    }

                    countRed != 0 -> {
                        val count = countRed + meRedCount
                        if (count == 1) {
                            val layoutParams = textView.layoutParams
                            layoutParams.width = 10.dp
                            layoutParams.height = 10.dp
                            textView.layoutParams = layoutParams
                            textView.text = ""
                        } else {
                            val layoutParams = textView.layoutParams
                            layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
                            textView.layoutParams = layoutParams
                            if (count >= 99) {
                                textView.text = "99+"
                            } else {
                                textView.text = count.toString()
                            }
                        }
                        textView.visibility = View.VISIBLE
                    }
                }

            }
        }

    }

    fun getCurrentIndex(): Int {
        return lastIndex
    }

    private var onClickCallBack: ((v: View, index: Int) -> Unit)? = null
    fun setOnClickCallBack(click: (v: View, index: Int) -> Unit) {
        onClickCallBack = click
    }

    private var onRefreshMain: (() -> Unit)? = null
    fun setRefreshMainBack(click: () -> Unit) {
        onRefreshMain = click
    }
}